package com.ysd.dao.impl;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.entity.XinWenLieBie;
import com.ysd.util.DBManger;

public class XinWenLieBieDaoImpl {
	public List<XinWenLieBie> getXinWenLieBieList(){
		List<XinWenLieBie> xwlbList = new ArrayList<XinWenLieBie>();
		
		Connection conn = DBManger.getConnection();
		Statement st=null;
		ResultSet rs=null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from xinwenfenliebiao_copy");
			while(rs.next()) {
				int xwflid=rs.getInt("xinWenFenLieId");
				String xwflName = rs.getString("xinWenFebLieName");
				int xwwjId=rs.getInt("xwwaiJianId");
				XinWenLieBie xwlb = new XinWenLieBie(xwflid, xwflName, xwwjId);
				xwlbList.add(xwlb);
			}
			return xwlbList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据分类id删除新闻分类
	 * 
	 * @param delxwId
	 * @return
	 */
		public int deleteXinWenFenLeiId(int delxwId) {
			return DBManger.executeUpdate("delete from  xinwenfenliebiao_copy where xinWenFenLieId = "+delxwId);

		}
		
		/**
		 * 添加的方法
		 */
		public int addXinWenFenLei(XinWenLieBie xw) {
			return DBManger.executeUpdate("insert into xinwenfenliebiao_copy values(null,'"+xw.getXinWenFebLieName()+"',null)");
			
			
		}
		
		/**
		 * 通过Name查找新闻分类
		 */
		public List<XinWenLieBie> getXWByName(String name){
			List<XinWenLieBie> xwList = new ArrayList<XinWenLieBie>();
			
			Connection conn = DBManger.getConnection();
			Statement st = null;
			ResultSet rs = null;
			
			try {
				st = conn.createStatement();
				rs=st.executeQuery("SELECT * from xinwenfenliebiao_copy where xinWenFebLieName ='"+name+"'");
				while(rs.next()) {
					String xinWenFebLieName = rs.getString("xinWenFebLieName");
					XinWenLieBie xw = new XinWenLieBie(0, xinWenFebLieName, 0);
					xwList.add(xw);
				}
				return xwList;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}finally {
				DBManger.closeAll(conn, st, rs);
			}
		}
		
		/**
		 * 回显数据
		 */
		public XinWenLieBie getXWFLById(int Id) {
			Connection conn = DBManger.getConnection();
			Statement  st = null;
			ResultSet rs = null;
			XinWenLieBie xw = null;
			try {
				st = conn.createStatement();
				rs = st.executeQuery("SELECT * from xinwenfenliebiao_copy where  xinWenFenLieId = "+Id);
				while(rs.next()) {
					int xinWenFenLieId = rs.getInt("xinWenFenLieId");
					String xinWenFebLieName = rs.getString("xinWenFebLieName");
					 xw = new XinWenLieBie(xinWenFenLieId, xinWenFebLieName, 0);
					
				}
				return xw;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}finally {
				DBManger.closeAll(conn, st, rs);
			}
			
		}
		/**
		 * 修改新闻分类
		 * 
		 */
		public int updateXWXL(XinWenLieBie xw) {
			String sql = "UPDATE xinwenfenliebiao_copy set xinWenFebLieName = '"+xw.getXinWenFebLieName()+"' where xinWenFenLieId="+xw.getXinWenFenLieId()+"";
			System.out.println(sql);
			return DBManger.executeUpdate(sql);
			
		}
		
		
		
		
		
		
		
		
		
		
		
		
}
